package org.focusj.randtext

import scala.io.Source
import java.io.File
import scala.util.Random

/**
 * 需要修改的问题，汉字使用unicode来生成。使用隐式转换将方法挂载到Random上
 */
object RandCharacters {
    val length = 20
    class RandCharacters() {
        val enDic = EnDic
        val noDic = NoDic
        val enDicLen = enDic.length
        val noDicLen = noDic.length
        val ZH = 0x9fa5 - 0x4e00 + 1

        def NoDic = Array[Char]('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
        def EnDic =
            Array[Char]('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
                'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')

        //生成随机的汉字
        def nextZhs(n: Int = length): Seq[Char] = {
            for (i <- 0 to n) yield {
                (0x4e00 + Random.nextDouble * ZH).toChar
            }
        }

        //FIXME:不知道为什么定义无参函数必须加括号
        def nextZhs(): Seq[Char] = nextZhs(length)

        //随机的数组
        def nextNos(n: Int = length): Seq[Char] = {
            for (i <- 0 to n) yield {
                NoDic(Random.nextInt(noDicLen))
            }
        }

        //随机的字母
        def nextEns(n: Int = length): Seq[Char] = {
            for (i <- 0 to n) yield {
                enDic(Random.nextInt(enDicLen))
            }
        }

        def nextScriptTag(): Seq[Char] = {
            val charSeq = nextZhs ++
                nextEns(Random.nextInt(length)) ++ nextNos(Random.nextInt(length))
            "<script>" ++ charSeq ++ "</script>"
        }
    }
    implicit def getRand(any: Random) = new RandCharacters
}

object Client2 {
    def main(args: Array[String]) {
        import RandCharacters._
        Random.nextZhs
        println(Random.nextScriptTag.mkString)
    }
}
